package com.example.bbgds.test.home.widget;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.example.bbgds.test.R;

/**
 * Created by zjzhu on 2016/4/26.
 */
public class AppItemViewHolder extends ItemViewHolder {
    private TextView mTextView;
    private FrameLayout mIndicatorLayout;

    public AppItemViewHolder(View itemView) {
        super(itemView);
        mTextView = (TextView) itemView.findViewById(R.id.name);
        mIndicatorLayout = (FrameLayout) itemView.findViewById(R.id.indicator);
    }

    @Override
    public void bindData(ItemInfo info, int poistion) {
        AppItemInfo appItemInfo = (AppItemInfo) info;
        if (mIndicatorLayout.getChildCount() != 0) {
            mIndicatorLayout.removeAllViews();
        }
        View indicatorView = appItemInfo.getStatusIndicatorView(itemView.getContext());
        if (indicatorView != null) {
            if (indicatorView.getParent() != null) {
                ViewGroup parent = (ViewGroup) indicatorView.getParent();
                parent.removeView(indicatorView);
            }
            mIndicatorLayout.addView(indicatorView);
        }
        mTextView.setCompoundDrawablesWithIntrinsicBounds(null, itemView.getContext().getResources().getDrawable(appItemInfo.getIconResId()), null, null);
        mTextView.setText(appItemInfo.getTitle());
        mTextView.setTextColor(itemView.getContext().getResources().getColor(appItemInfo.getTheme().getTextColorResId()));
        itemView.setBackgroundResource(appItemInfo.getTheme().getBackgroundResId());
    }

    public static AppItemViewHolder buildViewHolder(Context context) {
        View view = View.inflate(context, R.layout.app_normal, null);
        RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams((int) context.getResources().getDimension(R.dimen.app_item_witdh), RecyclerView.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(lp);
        return new AppItemViewHolder(view);
    }
}
